package view;

import Bean.Customer;
import control.CustomerManagerControl;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public final class EditCustomerDialog extends RootDialog
{

    CustomerManagerControl cmControl = new CustomerManagerControl();
    Customer Customer = new Customer();
    private int idOfRoomManagement;
    private view.CustomerCategoryPanel panel;
    private JFrame window;

    EditCustomerDialog(int idOfRoomManagement, view.CustomerCategoryPanel panel, JFrame window)
    {
        initComponents();
        this.panel = panel;
        this.setTitle("Chỉnh sửa khách hàng");
        this.idOfRoomManagement = idOfRoomManagement;
        setIntialValue(idOfRoomManagement);
        this.window = window;
        Init();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabel2 = new javax.swing.JLabel();
        jBChapnhan = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTFHoten = new javax.swing.JTextField();
        jTFNamsinh = new javax.swing.JTextField();
        jTFCMTND = new javax.swing.JTextField();
        jTFDienthoai = new javax.swing.JTextField();
        jTFDiachi = new javax.swing.JTextField();
        jCBGioitinh = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jBChapnhan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBChapnhan.setText("Chấp nhận");
        jBChapnhan.setFocusPainted(false);
        jBChapnhan.setFocusable(false);
        jBChapnhan.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBChapnhanActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 153));
        jLabel3.setText("Họ tên");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 51, 153));
        jLabel5.setText("CMND");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 153));
        jLabel6.setText("Giới tính");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(0, 51, 153));
        jLabel9.setText("Điện thoại");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 51, 153));
        jLabel10.setText("Địa chỉ");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(0, 51, 153));
        jLabel11.setText("Năm sinh");

        jTFHoten.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFNamsinh.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFCMTND.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFDienthoai.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFDiachi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jCBGioitinh.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jCBGioitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/edit_customer.png"))); // NOI18N
        jLabel1.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jBChapnhan, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(76, 76, 76))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jTFNamsinh, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jTFDienthoai, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTFDiachi)
                                    .addComponent(jTFCMTND, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jCBGioitinh, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTFHoten, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTFHoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCBGioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFNamsinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFCMTND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFDienthoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFDiachi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(18, 18, 18)
                        .addComponent(jBChapnhan, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBChapnhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBChapnhanActionPerformed
        Customer.setId(this.idOfRoomManagement);
        Customer.setName(jTFHoten.getText());
        Customer.setGender((String) jCBGioitinh.getSelectedItem());
        try
        {
            Customer.setBrithday(Integer.parseInt(jTFNamsinh.getText()));
            Customer.setCMTND(jTFCMTND.getText());
            Customer.setPhone(Long.parseLong(jTFDienthoai.getText()));
            Customer.setAdress(jTFDiachi.getText());
            if ((Customer.getBrithday() <= 0) || isNotNullNotEmptyNotWhiteSpace(jTFDiachi.getText()) == false || isNotNullNotEmptyNotWhiteSpace(jTFCMTND.getText()) == false || isNotNullNotEmptyNotWhiteSpace(jTFHoten.getText()) == false || (Customer.getBrithday() > 2012) || Customer.getName().equals("") || Customer.getBrithday() == 0 || Customer.getGender().equals("") || Customer.getCMTND().equals("") || Customer.getPhone() == 0 || Customer.getAdress().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
            }
            else
            {
                if (JOptionPane.showConfirmDialog(this, "Bạn chắc chắn muốn cập nhật?",
                        "Cập nhật khách hàng", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                {
                    cmControl.editCustomer(Customer);
                    JOptionPane.showMessageDialog(null, "Chỉnh sửa thành công");
                    window.setVisible(true);
                    window.setEnabled(true);
                    this.dispose();
                    panel.reset();

                }

            }
        }
        catch (NumberFormatException | HeadlessException ex)
        {
            JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
        }
    }//GEN-LAST:event_jBChapnhanActionPerformed

    public void setIntialValue(int choice)
    {
        if (choice != 0)
        {
            Customer = cmControl.getCustomer(choice);
            jTFHoten.setText(Customer.getName());
            jTFCMTND.setText(Customer.getCMTND());
            jTFNamsinh.setText(Integer.toString(Customer.getBrithday()));
            jTFDienthoai.setText(Long.toString(Customer.getPhone()));
            jTFDiachi.setText(Customer.getAdress());
            jCBGioitinh.setSelectedItem(Customer.getGender());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBChapnhan;
    private javax.swing.JComboBox jCBGioitinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTFCMTND;
    private javax.swing.JTextField jTFDiachi;
    private javax.swing.JTextField jTFDienthoai;
    private javax.swing.JTextField jTFHoten;
    private javax.swing.JTextField jTFNamsinh;
    // End of variables declaration//GEN-END:variables

    public static boolean isNotNullNotEmptyNotWhiteSpace(final String string)
    {
        return string != null && !string.isEmpty() && !string.trim().isEmpty();
    }
}
